import os

from django.conf import settings
from django.db import models

# Create your models here.
from common.constants import IMAGE_TYPE, TEXT_TYPE, VIDEO_TYPE
from common.file_utils import plat_file_upload_to, thumbnail_upload_to
from common.general_class import BaseModel


FILE_TYPE = [
    (IMAGE_TYPE, "图片"),
    (TEXT_TYPE, "文本"),
    (VIDEO_TYPE, "视频")
]
DOWNLOAD_WAIT = 0
DOWNLOADING = 1
DOWNLOAD_SUCCESS = 2
DOWNLOAD_FAIL = 3
FILE_DELETED = 3
FILE_FINISHED = 4
FILE_STATUS = [
    (DOWNLOAD_WAIT, "待下载"),
    (DOWNLOADING, "下载中"),
    (DOWNLOAD_SUCCESS, "可用"),
    (DOWNLOAD_FAIL, "下载失败"),
    (FILE_DELETED, "失效"),
    (FILE_FINISHED, "完成作品"),
]


class MaterialFile(BaseModel):
    name = models.CharField(max_length=32, blank=True, null=True, verbose_name="文件名")
    file_type = models.SmallIntegerField(choices=FILE_TYPE, verbose_name="文件类型")
    suffix = models.CharField(max_length=10, blank=True, null=True, verbose_name="文件后缀")
    source = models.CharField(max_length=15, blank=True, null=True, verbose_name="来源")
    # spec todo:规格字段，待补充
    # tag
    thumbnail = models.ImageField(upload_to=thumbnail_upload_to, verbose_name='缩略图', default='', blank=True)
    file = models.FileField(upload_to=plat_file_upload_to, blank=True, null=True, verbose_name="文件")
    status = models.SmallIntegerField(choices=FILE_STATUS, default=DOWNLOAD_SUCCESS, verbose_name="文件状态")
    note = models.TextField(blank=True, null=True, verbose_name="备注")

    def get_file_path(self, is_relative=False):
        if is_relative:
            return self.file.path
        return os.path.join(settings.BASE_DIR, self.file.path)

    def __str__(self):
        return f"{self.id}-{self.name}-类型：{self.suffix}"